package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.AttributeItemEntity;
import com.chushouya.product.dao.mapper.AttributeItemEntityMapper;
import com.chushouya.product.dto.admin.attribute.AttributeItemQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class AttributeItemRepository extends BaseRepository<AttributeItemEntityMapper, AttributeItemEntity, Long> {
    
    public List<AttributeItemEntity> selectList(AttributeItemQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    public PageResult<AttributeItemEntity> selectPageResult(AttributeItemQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }
    
    public AttributeItemEntity selectOne(AttributeItemQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    private Example buildExample(AttributeItemQuery query) {
        Example example = new Example(AttributeItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getAttrItemId())) {
            criteria.andEqualTo("attrItemId", query.getAttrItemId());
        }
        if (Objects.nonNull(query.getCategoryId())) {
            criteria.andEqualTo("categoryId", query.getCategoryId());
        }
        if (Strings.isNotEmpty(query.getCategoryName())) {
            criteria.andEqualTo("categoryName", query.getCategoryName());
        }
        if (Strings.isNotEmpty(query.getAttrName())) {
            criteria.andEqualTo("attrName", query.getAttrName());
        }
        if (Objects.nonNull(query.getCreateTimeStart())) {
            criteria.andGreaterThanOrEqualTo("createTime", query.getCreateTimeStart());
        }
        if (Objects.nonNull(query.getCreateTimeEnd())) {
            criteria.andLessThanOrEqualTo("createTime", query.getCreateTimeEnd());
        }
        example.orderBy("sort").asc();
        return example;
    }
    
    public List<AttributeItemEntity> selectListByCategoryId(Long categoryId) {
        AttributeItemQuery query = new AttributeItemQuery();
        query.setCategoryId(categoryId);
        return selectList(query);
    }
    
    public List<AttributeItemEntity> selectListByCategoryIds(List<Long> categoryIds) {
        if (Lists.isEmpty(categoryIds)) {
            return Lists.newArrayList();
        }
        Example example = new Example(AttributeItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("categoryId", categoryIds);
        example.orderBy("sort").asc();
        return this.selectByExample(example);
    }
}
